home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectSound / DeferredEffects / frmFX.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-08  |  17.3 KB  |  468 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form frmEffects 
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Audio Effects using DirectSound Buffers"
  6.    ClientHeight    =   4965
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   4740
  10.    Icon            =   "frmFX.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   4965
  14.    ScaleWidth      =   4740
  15.    StartUpPosition =   3  'Windows Default
  16.    Begin VB.Timer tmrUpdate 
  17.       Interval        =   50
  18.       Left            =   6180
  19.       Top             =   1620
  20.    End
  21.    Begin VB.CheckBox chkLoop 
  22.       Caption         =   "Loop Sound"
  23.       Height          =   315
  24.       Left            =   840
  25.       TabIndex        =   7
  26.       Top             =   4500
  27.       Width           =   1455
  28.    End
  29.    Begin VB.CommandButton cmdStop 
  30.       Caption         =   "&Stop"
  31.       Height          =   375
  32.       Left            =   3600
  33.       TabIndex        =   6
  34.       Top             =   4500
  35.       Width           =   1095
  36.    End
  37.    Begin VB.CommandButton cmdPlay 
  38.       Caption         =   "&Play"
  39.       Height          =   375
  40.       Left            =   2400
  41.       TabIndex        =   5
  42.       Top             =   4500
  43.       Width           =   1095
  44.    End
  45.    Begin VB.Frame fraEffects 
  46.       Caption         =   "Effects Information"
  47.       Height          =   3615
  48.       Left            =   120
  49.       TabIndex        =   1
  50.       Top             =   780
  51.       Width           =   4515
  52.       Begin VB.CommandButton cmdApply 
  53.          Caption         =   "Apply Effects"
  54.          Height          =   315
  55.          Left            =   2460
  56.          TabIndex        =   12
  57.          Top             =   3180
  58.          Width           =   1875
  59.       End
  60.       Begin VB.CommandButton cmdRemove 
  61.          Height          =   285
  62.          Left            =   2400
  63.          MaskColor       =   &H000000FF&
  64.          Picture         =   "frmFX.frx":0442
  65.          Style           =   1  'Graphical
  66.          TabIndex        =   11
  67.          Top             =   1920
  68.          UseMaskColor    =   -1  'True
  69.          Width           =   315
  70.       End
  71.       Begin VB.CommandButton cmdAdd 
  72.          Height          =   285
  73.          Left            =   2040
  74.          MaskColor       =   &H000000FF&
  75.          Picture         =   "frmFX.frx":0984
  76.          Style           =   1  'Graphical
  77.          TabIndex        =   10
  78.          Top             =   1920
  79.          UseMaskColor    =   -1  'True
  80.          Width           =   315
  81.       End
  82.       Begin VB.ListBox lstUse 
  83.          Height          =   840
  84.          Left            =   120
  85.          TabIndex        =   9
  86.          Top             =   2220
  87.          Width           =   4275
  88.       End
  89.       Begin VB.ListBox lstAvail 
  90.          Height          =   840
  91.          ItemData        =   "frmFX.frx":0EC6
  92.          Left            =   120
  93.          List            =   "frmFX.frx":0EE2
  94.          TabIndex        =   8
  95.          Top             =   1020
  96.          Width           =   4275
  97.       End
  98.       Begin VB.TextBox txtFile 
  99.          Height          =   285
  100.          Left            =   120
  101.          Locked          =   -1  'True
  102.          TabIndex        =   3
  103.          Text            =   "No file loaded..."
  104.          Top             =   480
  105.          Width           =   3975
  106.       End
  107.       Begin VB.CommandButton cmdBrowse 
  108.          Caption         =   "..."
  109.          Height          =   285
  110.          Left            =   4140
  111.          TabIndex        =   2
  112.          ToolTipText     =   "Open a new audio file..."
  113.          Top             =   480
  114.          Width           =   315
  115.       End
  116.       Begin VB.Label lbl 
  117.          BackStyle       =   0  'Transparent
  118.          Caption         =   "Available Effects"
  119.          Height          =   195
  120.          Index           =   3
  121.          Left            =   120
  122.          TabIndex        =   15
  123.          Top             =   780
  124.          Width           =   1215
  125.       End
  126.       Begin VB.Label lbl 
  127.          BackStyle       =   0  'Transparent
  128.          Caption         =   "Effects in use"
  129.          Height          =   195
  130.          Index           =   2
  131.          Left            =   120
  132.          TabIndex        =   14
  133.          Top             =   1980
  134.          Width           =   1215
  135.       End
  136.       Begin VB.Label lbl 
  137.          BackStyle       =   0  'Transparent
  138.          Caption         =   "Available Effects"
  139.          Height          =   195
  140.          Index           =   1
  141.          Left            =   180
  142.          TabIndex        =   13
  143.          Top             =   600
  144.          Width           =   1215
  145.       End
  146.       Begin VB.Label lbl 
  147.          BackStyle       =   0  'Transparent
  148.          Caption         =   "Currently loaded sound file:"
  149.          Height          =   195
  150.          Index           =   0
  151.          Left            =   120
  152.          TabIndex        =   4
  153.          Top             =   240
  154.          Width           =   4515
  155.       End
  156.    End
  157.    Begin MSComDlg.CommonDialog cdlOpen 
  158.       Left            =   300
  159.       Top             =   3720
  160.       _ExtentX        =   847
  161.       _ExtentY        =   847
  162.       _Version        =   393216
  163.    End
  164.    Begin VB.Label lbl 
  165.       BackStyle       =   0  'Transparent
  166.       Caption         =   "Audio Effects using Defered loading DirectSoundBuffers.  This allows you to check the status of effects before playing."
  167.       Height          =   615
  168.       Index           =   4
  169.       Left            =   660
  170.       TabIndex        =   0
  171.       Top             =   60
  172.       Width           =   3195
  173.    End
  174.    Begin VB.Image Image1 
  175.       Height          =   480
  176.       Left            =   120
  177.       Picture         =   "frmFX.frx":0F33
  178.       Top             =   180
  179.       Width           =   480
  180.    End
  181. Attribute VB_Name = "frmEffects"
  182. Attribute VB_GlobalNameSpace = False
  183. Attribute VB_Creatable = False
  184. Attribute VB_PredeclaredId = True
  185. Attribute VB_Exposed = False
  186. Option Explicit
  187. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  188. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  189. '  File:       frmFX.frm
  190. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  191. 'API declare for windows folder
  192. Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  193. Private Const mlMaxEffects As Long = 20
  194. 'Private declares for our DirectX objects
  195. Private dx As DirectX8
  196. Private ds As DirectSound8
  197. Private dsb As DirectSoundSecondaryBuffer8
  198. Private mlEffectKey As Long
  199. Private Sub cmdAdd_Click()
  200.     If lstAvail.ListIndex = -1 Then 'Nothing is selected
  201.         MsgBox "Please select an available effect before attempting to add it.", vbOKOnly Or vbInformation, "Nothing selected."
  202.         Exit Sub
  203.     End If
  204.     If lstUse.ListCount >= mlMaxEffects Then
  205.         MsgBox "You cannot add more than " & CStr(mlMaxEffects) & " effects in this sample.", vbOKOnly Or vbInformation, "No more effects."
  206.         Exit Sub
  207.     End If
  208.     'Add this item to our list of effects
  209.     lstUse.AddItem lstAvail.List(lstAvail.ListIndex) & " - Unallocated"
  210. End Sub
  211. Private Sub cmdApply_Click()
  212.     On Local Error GoTo NoFX
  213.     Dim DSEffects() As DSEFFECTDESC
  214.     Dim lResults() As Long
  215.     Dim lTempEffect As Long
  216.     Dim lCount As Long
  217.     'Do we have a sound buffer
  218.     If dsb Is Nothing Then
  219.         MsgBox "You must first load a wave file into a sound buffer before you can apply effects to it.", vbOKOnly Or vbInformation, "No buffer"
  220.         Exit Sub
  221.     End If
  222.     'Yup, now is there a sound already playing?
  223.     If (dsb.GetStatus And DSBSTATUS_PLAYING) = DSBSTATUS_PLAYING Then
  224.         MsgBox "Stop the currently playing sound before adding effects.", vbOKOnly Or vbInformation, "Sound is playing"
  225.         Exit Sub
  226.     End If
  227.     'Yes we do, do we have effects selected?
  228.     If lstUse.ListCount = 0 Then
  229.         If MsgBox("Do you want to turn off effects for this buffer?", vbYesNo Or vbQuestion, "No effects") = vbYes Then
  230.             'Calling SetFX with a count of 0 removes the effects from the buffer
  231.             dsb.SetFX 0, DSEffects, lResults
  232.             Exit Sub
  233.         Else
  234.             MsgBox "You must first select some effects to use.", vbOKOnly Or vbInformation, "No effects"
  235.             Exit Sub
  236.         End If
  237.     End If
  238.     'Ok, let's apply our effects info here
  239.     'First get an array of effects structs the right size
  240.     ReDim DSEffects(lstUse.ListCount - 1)
  241.     ReDim lResults(lstUse.ListCount - 1)
  242.     For lCount = 0 To lstUse.ListCount - 1
  243.         Select Case Left$(LCase(lstUse.List(lCount)), InStr(lstUse.List(lCount), " ") - 1)
  244.         Case "distortion"
  245.             lTempEffect = lTempEffect + (lCount + &H10)
  246.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_DISTORTION
  247.         Case "echo"
  248.             lTempEffect = lTempEffect + (lCount + &H20)
  249.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_ECHO
  250.         Case "chorus"
  251.             lTempEffect = lTempEffect + (lCount + &H40)
  252.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_CHORUS
  253.         Case "flanger"
  254.             lTempEffect = lTempEffect + (lCount + &H80)
  255.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_FLANGER
  256.         Case "compressor"
  257.             lTempEffect = lTempEffect + (lCount + &H100)
  258.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_COMPRESSOR
  259.         Case "gargle"
  260.             lTempEffect = lTempEffect + (lCount + &H200)
  261.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_GARGLE
  262.         Case "parameq"
  263.             lTempEffect = lTempEffect + (lCount + &H400)
  264.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_PARAMEQ
  265.         Case "wavesreverb"
  266.             lTempEffect = lTempEffect + (lCount + &H800)
  267.             DSEffects(lCount).guidDSFXClass = DSFX_STANDARD_WAVES_REVERB
  268.         End Select
  269.     Next
  270.     If mlEffectKey <> lTempEffect Then 'They don't match, set the fx.
  271.         dsb.SetFX lstUse.ListCount, DSEffects, lResults
  272.         'Now we can acquire the resources needed for these effects.
  273.         dsb.AcquireResources 0, lResults
  274.         Dim sNewItem As String
  275.         For lCount = 0 To lstUse.ListCount - 1
  276.             sNewItem = Left$(lstUse.List(lCount), InStr(lstUse.List(lCount), " ") - 1)
  277.             Select Case lResults(lCount)
  278.             Case DSFXR_FAILED
  279.                 lstUse.List(lCount) = sNewItem & " - Failed"
  280.             Case DSFXR_LOCHARDWARE
  281.                 lstUse.List(lCount) = sNewItem & " - Hardware"
  282.             Case DSFXR_LOCSOFTWARE
  283.                 lstUse.List(lCount) = sNewItem & " - Software"
  284.             Case DSFXR_UNALLOCATED
  285.                 lstUse.List(lCount) = sNewItem & " - Unallocated"
  286.             Case DSFXR_UNKNOWN
  287.                 lstUse.List(lCount) = sNewItem & " - Unknown"
  288.             Case DSFXR_PRESENT
  289.                 lstUse.List(lCount) = sNewItem & " - Present"
  290.             End Select
  291.         Next
  292.     End If
  293.     mlEffectKey = lTempEffect
  294.     Exit Sub
  295. NoFX:
  296.     MsgBox "This set of effects could not be set on this audio file.", vbOKOnly Or vbInformation, "Cannot set"
  297. End Sub
  298. Private Sub cmdBrowse_Click()
  299.     Static sCurDir As String
  300.     Dim desc As DSBUFFERDESC
  301.     'We want to open a file now
  302.     cdlOpen.flags = cdlOFNHideReadOnly Or cdlOFNFileMustExist
  303.     cdlOpen.Filter = "Wave Files (*.wav)|*.wav"
  304.     cdlOpen.FileName = vbNullString
  305.     If sCurDir = vbNullString Then
  306.         'Set the init folder to \windows\media if it exists.  If not, set it to the \windows folder
  307.         Dim sWindir As String
  308.         sWindir = Space$(255)
  309.         If GetWindowsDirectory(sWindir, 255) = 0 Then
  310.             'We couldn't get the windows folder for some reason, use the c:\
  311.             cdlOpen.InitDir = "C:\"
  312.         Else
  313.             Dim sMedia As String
  314.             sWindir = Left$(sWindir, InStr(sWindir, Chr$(0)) - 1)
  315.             If Right$(sWindir, 1) = "\" Then
  316.                 sMedia = sWindir & "Media"
  317.             Else
  318.                 sMedia = sWindir & "\Media"
  319.             End If
  320.             'We are trying to find the windows\media directory.  If it
  321.             'doesn't exist, then use the windows folder as a default
  322.             If Dir$(sMedia, vbDirectory) <> vbNullString Then
  323.                 cdlOpen.InitDir = sMedia
  324.             Else
  325.                 cdlOpen.InitDir = sWindir
  326.             End If
  327.         End If
  328.     Else
  329.         'No need to move folders.  Stay where they picked the last file
  330.         cdlOpen.InitDir = sCurDir
  331.     End If
  332.     On Local Error GoTo ClickedCancel
  333.     cdlOpen.CancelError = True
  334.     cdlOpen.ShowOpen   ' Display the Open dialog box
  335.     'Save the current information
  336.     sCurDir = GetFolder(cdlOpen.FileName)
  337.             
  338.     On Local Error GoTo NoLoadSegment
  339.     'Before we load the buffer stop one if it's playing
  340.     If Not (dsb Is Nothing) Then If dsb.GetStatus = DSBSTATUS_PLAYING Then dsb.Stop
  341.     'We need to set the CTRLFX flag so we can control the effects on this object
  342.     'We pass the LOCDEFER flag so we can acquire the
  343.     'resources for the effects before we play them
  344.     desc.lFlags = DSBCAPS_CTRLFX Or DSBCAPS_LOCDEFER
  345.     'Now let's load the segment
  346.     Set dsb = ds.CreateSoundBufferFromFile(cdlOpen.FileName, desc)
  347.     mlEffectKey = 0
  348.     txtFile.Text = cdlOpen.FileName
  349.     Exit Sub
  350. NoLoadSegment:
  351.     If Err.Number = DSERR_BUFFERTOOSMALL Then 'This buffer isn't big enough to control effects on
  352.         MsgBox "This file isn't long enough to control effects.  Please choose a longer audio file.", vbOKOnly Or vbCritical, "Couldn't load"
  353.     Else 'Some other error
  354.         MsgBox "Couldn't load this file", vbOKOnly Or vbCritical, "Couldn't load"
  355.     End If
  356. ClickedCancel:
  357. End Sub
  358. Private Sub cmdPlay_Click()
  359.     If dsb Is Nothing Then
  360.         MsgBox "You must first load a wave file into a sound buffer before you can play it.", vbOKOnly Or vbInformation, "No buffer"
  361.         Exit Sub
  362.     End If
  363.     dsb.Play chkLoop.Value
  364.     EnablePlayUI False
  365. End Sub
  366. Private Sub cmdRemove_Click()
  367.     If lstUse.ListIndex = -1 Then 'Nothing is selected
  368.         MsgBox "Please select an effect that's being used before attempting to remove it.", vbOKOnly Or vbInformation, "Nothing selected."
  369.         Exit Sub
  370.     End If
  371.     'Add this item to our list of effects
  372.     lstUse.RemoveItem lstUse.ListIndex
  373. End Sub
  374. Private Sub cmdSave_Click()
  375.     On Error GoTo ClickedCancel
  376.     With cdlOpen
  377.         .InitDir = GetFolder(txtFile.Text)
  378.         .FileName = txtFile.Text
  379.         .CancelError = True
  380.         .ShowSave
  381.         dsb.SaveToFile .FileName
  382.     End With
  383.     Exit Sub
  384. ClickedCancel:
  385. End Sub
  386. Private Sub cmdStop_Click()
  387.     If dsb Is Nothing Then
  388.         MsgBox "You must first load a wave file into a sound buffer before you can stop it.", vbOKOnly Or vbInformation, "No buffer"
  389.         Exit Sub
  390.     End If
  391.     dsb.Stop
  392.     'Stop doesn't reset the current position
  393.     dsb.SetCurrentPosition 0
  394.     EnablePlayUI True
  395. End Sub
  396. Private Sub Form_Load()
  397.     EnablePlayUI True
  398.     InitDSound
  399. End Sub
  400. Private Sub Form_Unload(Cancel As Integer)
  401.     CleanupDSound
  402. End Sub
  403. Private Sub InitDSound()
  404.     On Error GoTo FailedInit
  405.     Set dx = New DirectX8
  406.     'Create our default DirectSound object
  407.     Set ds = dx.DirectSoundCreate(vbNullString)
  408.     ds.SetCooperativeLevel Me.hWnd, DSSCL_NORMAL
  409.     Exit Sub
  410. FailedInit:
  411.     MsgBox "Could not initialize DirectSound." & vbCrLf & "This sample will exit.", vbOKOnly Or vbInformation, "Exiting..."
  412.     Unload Me
  413. End Sub
  414. Private Sub CleanupDSound()
  415.     'Let's clean up now
  416.     If Not dsb Is Nothing Then
  417.         'iF we are playing our file, stop it
  418.         If dsb.GetStatus = DSBSTATUS_PLAYING Then dsb.Stop
  419.         'Destroy our objects
  420.         Set dsb = Nothing
  421.     End If
  422.     Set ds = Nothing
  423.     Set dx = Nothing
  424. End Sub
  425. Private Function GetFolder(ByVal sFile As String) As String
  426.     Dim lCount As Long
  427.     For lCount = Len(sFile) To 1 Step -1
  428.         If Mid$(sFile, lCount, 1) = "\" Then
  429.             GetFolder = Left$(sFile, lCount)
  430.             Exit Function
  431.         End If
  432.     Next
  433.     GetFolder = vbNullString
  434. End Function
  435. Private Sub lstAvail_DblClick()
  436.     'Double clicking should be the same as clicking the 'Add' button
  437.     cmdAdd_Click
  438. End Sub
  439. Private Sub lstUse_DblClick()
  440.     'Double clicking should be the same as clicking the 'Remove' button
  441.     cmdRemove_Click
  442. End Sub
  443. Private Sub EnablePlayUI(ByVal fEnable As Boolean)
  444.     On Error Resume Next
  445.     If fEnable Then
  446.         chkLoop.Enabled = True
  447.         cmdPlay.Enabled = True
  448.         cmdStop.Enabled = False
  449.         cmdBrowse.Enabled = True
  450.         cmdPlay.SetFocus
  451.     Else
  452.         chkLoop.Enabled = False
  453.         cmdPlay.Enabled = False
  454.         cmdStop.Enabled = True
  455.         cmdBrowse.Enabled = False
  456.         cmdStop.SetFocus
  457.     End If
  458. End Sub
  459. Private Sub tmrUpdate_Timer()
  460.     If Not (dsb Is Nothing) Then
  461.         If (dsb.GetStatus And DSBSTATUS_PLAYING) <> DSBSTATUS_PLAYING Then
  462.             If cmdPlay.Enabled = False Then
  463.                 EnablePlayUI True
  464.             End If
  465.         End If
  466.     End If
  467. End Sub
  468.